package dao.impl;

import dao.BaseDAO;
import dao.UserDAO;
import entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by asus on 2017/6/17.
 */
@Repository("userDAO")
public class UserDAOImpl extends BaseDAO implements UserDAO{

    /**
     * User的UserMapper映射类
     */
    private class UserMapper implements RowMapper<User> {
        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException {
            User user = new User();
            user.setId(resultSet.getLong("id"));
            user.setName(resultSet.getString("name"));
            user.setPwd(resultSet.getString("pwd"));
            return user;
        }
    }

    /**
     * 根据用户名、密码查询用户，用于登录
     *
     * @param name 用户名
     * @param pwd  密码
     * @return 查询到的唯一用户实体
     */
    @Override
    public User findUser(String name, String pwd) {
        String sql = "select * from auth_user where name=? and pwd=?";
        try {
            return (User) jdbcTemplate.queryForObject(sql, new Object[]{name, pwd}, new UserMapper());
        } catch (Exception e) {
            //logger

            return null;
        }
    }

    @Override
    public void save(User user) {
        String sql = "insert into auth_user(id,name,pwd) values(?,?,?)";
        jdbcTemplate.update(sql, user.getId(), user.getName(), user.getPwd());
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from auth_user where id=?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public void update(User user) {
        String sql = "update auth_user set name=?,pwd=? where id=?";
        jdbcTemplate.update(sql, user.getName(), user.getPwd(), user.getId());
    }

    @Override
    public User findById(Long id) {
        String sql = "select * from auth_user where id=?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new UserMapper());
    }

    @Override
    public Collection<User> findByIds(Collection<Long> ids) {
        System.out.println(jdbcTemplate);
        StringBuilder sqlStr = new StringBuilder("select * from auth_user where id in (");
        for (Long id : ids) {
            sqlStr.append(id + ",");
        }
        sqlStr.append(")");
        String sql = sqlStr.deleteCharAt(sqlStr.length() - 2).toString();
        System.out.println(sql);
        try {
            return jdbcTemplate.query(sql, new UserMapper());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Collection<User> fingPage(int page, int size) {
        if (page < 1) {
            page = 1;
        }
        if (size < 0) {
            size = 20;
        }
        String sql = "select * from auth_user limit ?,?";
        int skip = (page - 1) * size;
        return jdbcTemplate.query(sql, new Object[]{skip, size}, new UserMapper());
    }

    @Override
    public List<User> findAllUsers() {
        String sql="select * from auth_user";
        return jdbcTemplate.query(sql,new UserMapper());
    }

    @Override
    public Integer getCount() {
        String sql="select count(*) from auth_user";
        try{
            return jdbcTemplate.queryForObject(sql,Integer.class);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    public static void main(String[] args){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDAOImpl userDAO= (UserDAOImpl) applicationContext.getBean("userDAOImpl");
        List<Long> ids=new ArrayList<>();
        ids.add(1L);
        ids.add(2L);
        System.out.println(userDAO.findByIds(ids));
    }
}
